package com.hua.customviews.widget

import android.annotation.SuppressLint
import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import com.hua.customviews.databinding.LoadingHeaderLayoutBinding
import com.scwang.smart.refresh.classics.ClassicsAbstract
import com.scwang.smart.refresh.layout.api.RefreshHeader
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.constant.RefreshState

@Suppress("CanBeParameter")
class LoadingHeader(private val mContext: Context, private val attrs: AttributeSet?, defStyleAttr: Int) :
    ClassicsAbstract<LoadingHeader>(mContext, attrs, defStyleAttr), RefreshHeader {
    init {
        initView()
    }

    constructor(mContext: Context, attrs: AttributeSet?) : this(mContext, attrs, 0) {
        initView()
    }

    constructor(mContext: Context) : this(mContext, null, 0) {
        initView()
    }

    private val mBinding =
        LoadingHeaderLayoutBinding.inflate(LayoutInflater.from(mContext), this, true).apply {
            if (isInEditMode) { //编辑模式预览
                lavLoading.visibility = VISIBLE
                tvFinishTip.visibility = GONE
            }
        }
    var mCustomView: View? = null

    //供外部处理相关逻辑监听
    private var onStateChangeListener: ((refreshLayout: RefreshLayout, oldState: RefreshState, newState: RefreshState, header: LoadingHeader) -> Unit)? =
        null
    private var onFinishedListener: ((refreshLayout: RefreshLayout, success: Boolean) -> Unit)? = null

    fun setOnStateChangeListener(onStateChangeListener: (refreshLayout: RefreshLayout, oldState: RefreshState, newState: RefreshState, header: LoadingHeader) -> Unit) {
        this.onStateChangeListener = onStateChangeListener
    }

    fun setOnFinishedListener(onFinishedListener: (refreshLayout: RefreshLayout, success: Boolean) -> Unit) {
        this.onFinishedListener = onFinishedListener
    }

    private fun initView() {
        if (mCustomView != null) {
            this.removeAllViews()
            this.addView(mCustomView)
        }
    }

    override fun invalidate() {
        initView()
        super.invalidate()
    }


    override fun onFinish(refreshLayout: RefreshLayout, success: Boolean): Int {
        if (mCustomView == null) {
            mBinding.lavLoading.apply {
                cancelAnimation()
                frame = 0
                visibility = GONE
            }
            mBinding.tvFinishTip.visibility = VISIBLE
        }
        if (onFinishedListener != null) onFinishedListener?.invoke(refreshLayout, success)
        //返回刷新完成后，延时回弹时间
        return mFinishDuration //不要super执行
    }

    override fun getView(): View {
        return this//不要super执行
    }

    override fun onStartAnimator(refreshLayout: RefreshLayout, height: Int, maxDragHeight: Int) {
        //不要super执行onStartAnimator
    }

    @SuppressLint("MissingSuperCall")
    override fun onDetachedFromWindow() {
        //不要super执行
    }

    override fun onStateChanged(
        refreshLayout: RefreshLayout,
        oldState: RefreshState,
        newState: RefreshState
    ) {
        if (onStateChangeListener != null) onStateChangeListener?.invoke(
            refreshLayout,
            oldState,
            newState,
            this@LoadingHeader
        )
        if (mCustomView == null) when (newState) {
            //回到初始状态
            RefreshState.None -> {
            }
            //开始下拉
            RefreshState.PullDownToRefresh -> {
                mBinding.tvFinishTip.visibility = GONE
                mBinding.lavLoading.pauseAnimation()
                mBinding.lavLoading.visibility = VISIBLE
            }
            //达到释放刷新条件
            RefreshState.ReleaseToRefresh -> {

            }
            //达到刷新条件后松手释放
            RefreshState.RefreshReleased -> {
                mBinding.lavLoading.resumeAnimation()
            }
            //刷新中
            RefreshState.Refreshing -> {

            }
            //达到下拉上二楼条件
            RefreshState.ReleaseToTwoLevel -> {

            }

            RefreshState.Loading -> {

            }

            else -> {}
        }

        super.onStateChanged(refreshLayout, oldState, newState)
    }

}